package com.tianyang.yukuaifuzs.business.main.presenter;

import com.tianyang.yukuaifuzs.business.auth.bean.MerchantVerifyOutput;
import com.tianyang.yukuaifuzs.business.auth.model.AuthModel;
import com.tianyang.yukuaifuzs.business.main.contract.MerchantInfoAuthContract;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：商户信息支付宝、微信授权
 * Created by xyj on 2024/11/15
 */
public class MerchantInfoAuthPresenter implements MerchantInfoAuthContract.Presenter {

    private MerchantInfoAuthContract.View mView;

    private CompositeSubscription mSubscription;

    public MerchantInfoAuthPresenter(MerchantInfoAuthContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMerchantVerify(String phone) {
        mView.showLoadingDialog(false);
        Subscription subscription = AuthModel.getInstance().yplMerchantVerify(phone)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MerchantVerifyOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(MerchantVerifyOutput output) {
                        if (output != null) {
                            mView.showMerchantVerifyView(output);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
